package approve.action;

import java.util.ArrayList;
import java.util.List;

import memo.action.Memo_listview;
import model.pojo.Employee;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.LayoutInflater.Factory;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import approve.adapter.ApproveDocumentGeneralNotShowAdapter;
import approve.dao.ApproveDocDao;
import approve.model.Approvedoc;

public class App_ReferenceDoc_Waiting extends Activity{

	TextView logout,username,home,welcome;
	TextView la_headerTitle;
	ListView list;
	Button buttonBack;

	List<Approvedoc> lstApprove = new ArrayList<Approvedoc>();
	ApproveDocumentGeneralNotShowAdapter adapter;
	String flagsort="DESC";
	final Handler uiHandler2=new Handler();
	boolean isUpdateRequired2=false;
	TextView tvwaiting;
	LayoutInflater inflaterwaiting;
	View textEntryViewwaiting;
	AlertDialog.Builder builderwaiting;
	AlertDialog alertwaiting2;
	
	public boolean isUpdateRequired2() {
		return isUpdateRequired2;
	}

	public void setUpdateRequired2(boolean isUpdateRequired2) {
		this.isUpdateRequired2 = isUpdateRequired2;
	}

	public AlertDialog getAlertwaiting2() {
		return alertwaiting2;
	}

	public void setAlertwaiting2(AlertDialog alertwaiting2) {
		this.alertwaiting2 = alertwaiting2;
	}

	public Handler getUiHandler2() {
		return uiHandler2;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try{
			initall();
		}catch(Exception e){
			e.printStackTrace();
			Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		}
	}
	
	private void initall(){
		setContentView(R.layout.app_referencedoc_waiting);
		getInitAttribute();
		loadlistview();

		RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relativeTitle);

		Resources res = getResources();
		final BitmapDrawable background = (BitmapDrawable) res.getDrawable(R.drawable.bg_menu_item);
		background.setTileModeX(Shader.TileMode.REPEAT);
		background.setTileModeY(Shader.TileMode.REPEAT);
		background.setDither(true);
		relativeLayout.setBackgroundDrawable(background);
		buttonBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				buttonBack.setBackgroundResource(R.drawable.bg_back_pressed);
				Intent intent = new Intent(App_ReferenceDoc_Waiting.this,MainEApproval.class);
				startActivity(intent);	
				finish();
			}
		});
	}

	void loadlistview(){
		getAllListApprovePendingDoc();
		adapter = new ApproveDocumentGeneralNotShowAdapter(App_ReferenceDoc_Waiting.this, lstApprove,LoginActivity.screenwidth,LoginActivity.lang.getString("created-date"),"referdocwaiting");
		list.setAdapter(adapter);
		la_headerTitle.setText(LoginActivity.lang.getString("reference_document")+"-"+LoginActivity.lang.getString("empNotApprove")+" ("+lstApprove.size()+"):");
	}

	private void getInitAttribute(){
		logout=(TextView)findViewById(R.id.logout);
		logout.setText(LoginActivity.lang.getString("Logout"));

		username=(TextView)findViewById(R.id.username);
		username.setText(LoginActivity.emp.getUserName());

		welcome = (TextView)findViewById(R.id.welcome);
		welcome.setText(LoginActivity.lang.getString("ac_Welcome"));

		list = (ListView)findViewById(R.id.receiverreferencedoc_waitinglist);

		logout.setOnClickListener(listenerLogout);

		buttonBack = (Button)findViewById(R.id.buttonBack);

		la_headerTitle=(TextView)findViewById(R.id.la_headerTitle);
		inflaterwaiting = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);          
		textEntryViewwaiting = inflaterwaiting.inflate(R.layout.waiting, null);
		tvwaiting=(TextView)textEntryViewwaiting.findViewById(R.id.TextView01);					
		tvwaiting.setText(LoginActivity.lang.getString("waiting")+"...");

		builderwaiting = new AlertDialog.Builder(App_ReferenceDoc_Waiting.this);
		builderwaiting.setView(textEntryViewwaiting);
		builderwaiting.setCancelable(false);
		alertwaiting2 = builderwaiting.create();
	}

	// get all list approve pending document
	public void getAllListApprovePendingDoc(){
		lstApprove = ApproveDocDao.getAllReferenceDocGeneral(LoginActivity.database, LoginActivity.emp.getIdemployee(), 2,flagsort);
		if(lstApprove.size() <= 0){
			TextView empltyList=(TextView) findViewById(R.id.empltyList);
			empltyList.setText(LoginActivity.lang.getString("no_data"));
		}
	}
	public void loadAllReferWaiting(Approvedoc approvedoc){
		if(approvedoc.getiDTypeDoc()<11){
			Intent intent=new Intent(App_ReferenceDoc_Waiting.this,App_ViewForm_LessThan10.class);	
			intent.putExtra("approvedoc", approvedoc);
			intent.putExtra("functionview", "exec");
			intent.putExtra("parentview", "referWait");
			startActivity(intent);
		}	
		else if(approvedoc.getiDTypeDoc()==12){
			Intent intent=new Intent(App_ReferenceDoc_Waiting.this,App_ViewForm12.class);	
			intent.putExtra("approvedoc", approvedoc);
			intent.putExtra("functionview", "exec");
			intent.putExtra("parentview", "referWait");
			startActivity(intent);
		}	
		else if(approvedoc.getiDTypeDoc()==13){
			Intent intent=new Intent(App_ReferenceDoc_Waiting.this,App_ViewForm13.class);	
			intent.putExtra("approvedoc", approvedoc);
			intent.putExtra("functionview", "exec");
			intent.putExtra("parentview", "referWait");
			startActivity(intent);
		}	
		else if(approvedoc.getiDTypeDoc()==14){
			Intent intent=new Intent(App_ReferenceDoc_Waiting.this,App_ViewForm14.class);	
			intent.putExtra("approvedoc", approvedoc);
			intent.putExtra("functionview", "exec");
			intent.putExtra("parentview", "referWait");
			startActivity(intent);
		}	
		else if(approvedoc.getiDTypeDoc()==15){
			Intent intent=new Intent(App_ReferenceDoc_Waiting.this,App_ViewForm15.class);	
			intent.putExtra("approvedoc", approvedoc);
			intent.putExtra("functionview", "exec");
			intent.putExtra("parentview", "referWait");
			startActivity(intent);
		}	
		else if(approvedoc.getiDTypeDoc()==16){
			Intent intent=new Intent(App_ReferenceDoc_Waiting.this,App_ViewForm16.class);	
			intent.putExtra("approvedoc", approvedoc);
			intent.putExtra("functionview", "exec");
			intent.putExtra("parentview", "referWait");
			startActivity(intent);
		}	
		else if(approvedoc.getiDTypeDoc()==17){
			Intent intent=new Intent(App_ReferenceDoc_Waiting.this,App_ViewForm17.class);	
			intent.putExtra("approvedoc", approvedoc);
			intent.putExtra("functionview", "exec");
			intent.putExtra("parentview", "referWait");
			startActivity(intent);
		}	

		finish();
	}

	private OnClickListener listenerLogout=new OnClickListener() {

		public void onClick(View v) {
			logout.setTextColor(Color.parseColor("#1188ff"));
			LoginActivity.flagruntimer=true;
			LoginActivity.soluong=0;
			LoginActivity.emp=new Employee();
			MainEApproval.mNotificationManager.cancel(MainEApproval.SIMPLE_NOTFICATION_ID);
			MainEApproval.timer.cancel();
			Intent i=new Intent(App_ReferenceDoc_Waiting.this, LogoutActivity.class);
			startActivity(i);
			finish();
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = new MenuInflater(getApplicationContext());
		if(LoginActivity.typeLang.equals("EN")){
			inflater.inflate(R.menu.menu_sort, menu);
		}else if(LoginActivity.typeLang.equals("VN")){
			inflater.inflate(R.menu.menu_sortvn, menu);
		}else{
			inflater.inflate(R.menu.menu_sortkr, menu);
		}
		setMenuBackground();
		return true;

	}

	protected void setMenuBackground(){
		Log.d("", "Enterting setMenuBackGround");  
		getLayoutInflater().setFactory( new Factory() {  

			@Override
			public View onCreateView(String name, Context context,
					AttributeSet attrs) {
				if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {  

					try { 
						LayoutInflater f = getLayoutInflater();  
						final View view = f.createView( name, null, attrs );  
						new Handler().post( new Runnable() {  
							public void run () {  
								view.setBackgroundResource( R.drawable.border_footer);  
							}  
						} );  
						return view;  
					}  
					catch ( InflateException e ) {}  
					catch ( ClassNotFoundException e ) {}  
				}  
				return null;  

			}  
		});  
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if(LoginActivity.typeLang.equals("VN")){
			switch (item.getItemId()) {
			case R.id.menu_eappvn:
				processMenuEApproval();
				return true;
			case R.id.menu_primemovn:
				processMenuPrivateMemo();
				return true;
			case R.id.sortvn:
				sort();
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}else if(LoginActivity.typeLang.equals("KR")){
			switch (item.getItemId()) {
			case R.id.menu_eappkr:
				processMenuEApproval();
				return true;
			case R.id.menu_primemokr:
				processMenuPrivateMemo();
				return true;
			case R.id.sortkr:
				sort();
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}else{
			switch (item.getItemId()) {
			case R.id.menu_eapp:
				processMenuEApproval();
				return true;
			case R.id.menu_primemo:
				processMenuPrivateMemo();
				return true;
			case R.id.sort:
				sort();
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}

	}

	void sort(){
		if(flagsort.equals("DESC")){
			flagsort="ASC";
		}else{
			flagsort="DESC";
		}	
		loadlistview();
	}

	@Override
	public void onBackPressed() {
		alertwaiting2.show();
		try{
			new Thread(){
				public void run(){
					Intent intent = new Intent(App_ReferenceDoc_Waiting.this,MainEApproval.class);
					startActivity(intent);
					uiHandler2.post(new Runnable() {
						@Override
						public void run() {
							if(isUpdateRequired2){
							}else{
								alertwaiting2.cancel();
							}
						}
					});
					finish();
				}
			}.start();
		}catch (Exception e){}
	}

	public void processMenuEApproval(){
		Intent intent = new Intent(App_ReferenceDoc_Waiting.this,MainEApproval.class);
		startActivity(intent);
		finish();
	}

	public void processMenuPrivateMemo(){
		Intent intent = new Intent(App_ReferenceDoc_Waiting.this,Memo_listview.class);
		startActivity(intent);
		finish();
	}
}